package com.bkgy.controller.qualityDaily.controller;

import com.bkgy.controller.qualityDaily.entity.QualitydailyQuestion;
import com.bkgy.controller.qualityDaily.entity.pdo.QuestionQueryReq;
import com.bkgy.controller.qualityDaily.entity.pdo.QuestionSaveReq;
import com.bkgy.controller.qualityDaily.entity.pdo.QuestionSaveTable;
import com.bkgy.controller.qualityDaily.service.IQualitydailyQuestionService;
import com.bkgy.sys.common.entity.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Tag(name = "质量日报-质量问题情况")
@Controller
@RequestMapping("/qualityquestion")
public class QualitydailyQuestionController {

    @Autowired
    private IQualitydailyQuestionService service;

    @ResponseBody
    @Operation(summary = "质量问题情况三个柱形图", description = "质量日报》三冷区域》质量问题情况柱形图")
    @PostMapping("/questionBarRep")
    public ResultData questionBarRep() {
        return service.questionBarRep();
    }

    @ResponseBody
    @Operation(summary = "质量问题情况报表", description = "质量日报》三冷区域》质量问题情况")
    @PostMapping("/questionRep")
    public ResultData questionRep(@RequestBody QuestionQueryReq obj) {
        obj.setDbegin(obj.getDbegin().replaceAll("-",""));
        obj.setDend(obj.getDend().replaceAll("-",""));
        return service.questionRep(obj);
    }

    @ResponseBody
    @Operation(summary = "新增", description = "质量日报》质量填报》质量问题情况》新增")
    @PostMapping("/qualitydailyQuestionadd")
    public ResultData<String> savequalityDaily(@RequestBody QuestionSaveReq req) {
        QualitydailyQuestion obj = new QualitydailyQuestion();
        obj.setId(null);
        obj.setYearMonthDay(req.getYearMonthDay());
        obj.setArea(req.getArea());
        obj.setCreateTime(new Date());
        List<QuestionSaveTable> list = req.getQuestionTable();
        for (int i = 0; i < list.size(); i++) {
            try {
                QuestionSaveTable q = list.get(i);
                obj.getClass().getMethod("setCol" + i + 0, BigDecimal.class).invoke(obj, q.getOne());
                obj.getClass().getMethod("setCol" + i + 1, BigDecimal.class).invoke(obj, q.getTwo());
                obj.getClass().getMethod("setCol" + i + 2, BigDecimal.class).invoke(obj, q.getThree());
                obj.getClass().getMethod("setCol" + i + 3, BigDecimal.class).invoke(obj, q.getCompanyone());
                obj.getClass().getMethod("setCol" + i + 4, BigDecimal.class).invoke(obj, q.getCompanytwo());
                obj.getClass().getMethod("setCol" + i + 5, BigDecimal.class).invoke(obj, q.getCompanythree());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return service.saveQualitydailyQuestion(obj);
    }

    @ResponseBody
    @Operation(summary = "修改", description = "质量日报》质量填报》质量问题情况》修改")
    @PostMapping("/qualitydailyQuestionupdate")
    public ResultData<String> updatequalityDaily(@RequestBody QuestionSaveReq req) {
        List<QuestionSaveTable> list = req.getQuestionTable();
        QualitydailyQuestion obj = new QualitydailyQuestion();
        obj.setId(req.getId());
        obj.setYearMonthDay(req.getYearMonthDay());
        obj.setArea(req.getArea());
        for (int i = 0; i < list.size(); i++) {
            try {
                QuestionSaveTable q = list.get(i);
                obj.getClass().getMethod("setCol" + i + 0, BigDecimal.class).invoke(obj, q.getOne());
                obj.getClass().getMethod("setCol" + i + 1, BigDecimal.class).invoke(obj, q.getTwo());
                obj.getClass().getMethod("setCol" + i + 2, BigDecimal.class).invoke(obj, q.getThree());
                obj.getClass().getMethod("setCol" + i + 3, BigDecimal.class).invoke(obj, q.getCompanyone());
                obj.getClass().getMethod("setCol" + i + 4, BigDecimal.class).invoke(obj, q.getCompanytwo());
                obj.getClass().getMethod("setCol" + i + 5, BigDecimal.class).invoke(obj, q.getCompanythree());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return service.updateQualitydailyQuestion(obj);
    }

    @ResponseBody
    @Operation(summary = "删除", description = "质量日报》质量填报》质量问题情况》删除")
    @PostMapping("/qualitydailyQuestiondel")
    public ResultData<String> deletequalityDaily(@RequestBody QuestionSaveReq req) {
        QualitydailyQuestion obj = new QualitydailyQuestion();
        obj.setId(req.getId());
        obj.setYearMonthDay(req.getYearMonthDay());
        obj.setArea(req.getArea());
        return service.deleteQualitydailyQuestion(obj);
    }

    @ResponseBody
    @Operation(summary = "分页查询", description = "质量日报》质量填报》质量问题情况》分页查询")
    @PostMapping("/getQualitydailyQuestionPage")
    public ResultData<String> selectPage(@RequestBody QuestionQueryReq obj) {
        return service.selectPage(obj);
    }
}
